package cn.example.freid.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.example.freid.util.JDBCUtil;
import cn.example.freid.util.ResultsetUtil;
import cn.example.freid.util.SQLStrings;
import cn.example.freid.util.StringUtil;


/**
 * Servlet implementation class QueryFacilitator
 */
@WebServlet("/Query.do")
public class QueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";
		
		Object role = request.getSession().getAttribute("role");
		
		if(role != null && role.equals("admin")) {
			
			String method = request.getParameter("method");
			JDBCUtil jdbcUtil = null;
			try {
				if (method.equals("all")) {
					
					jdbcUtil = new JDBCUtil();
					ResultSet rs = jdbcUtil.query(SQLStrings.QUERY_ALL_SQL);
					msg = ResultsetUtil.resultsetToJson(rs);
					err = "0";
					
				} else if (method.equals("byMail")) {
					
					String mail = request.getParameter("domainMail");
					if (StringUtil.isNullOrEmpty(mail)) {
						msg = "classID is null or empty";
					} else {
						
						jdbcUtil = new JDBCUtil();
						ResultSet rs = jdbcUtil.query(SQLStrings.QUERY_BYID_SQL, mail);
						msg = ResultsetUtil.resultsetToJson(rs);
						err = "0";
					}
					
				} else if (method.equals("byPage")) {
					
					int pageSize = 12;
					int pageNo = 1;
					
					String pageNoString = request.getParameter("pageNo");
					String pageSizeString = request.getParameter("pageSize");
					
					if (!StringUtil.isNullOrEmpty(pageSizeString))
						pageSize = Integer.parseInt(pageSizeString);
					
					if (!StringUtil.isNullOrEmpty(pageNoString))
						pageNo = Integer.parseInt(pageNoString);
					
					int startNo = (pageNo - 1) * pageSize;
					
					jdbcUtil = new JDBCUtil();
					ResultSet rs = jdbcUtil.query(
							SQLStrings.QUERY_BYPAGE_SQL, startNo, pageSize);
					msg = ResultsetUtil.resultsetToJson(rs);
					
				} else {
					msg = "invalid method";
				}
			} catch (SQLException e){
				e.printStackTrace();
				msg = e.getMessage();
			} catch (InstantiationException e) {
				e.printStackTrace();
				msg = e.getMessage();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				msg = e.getMessage();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				msg = e.getMessage();
			} finally {
				
				if (jdbcUtil != null) {
					try {
						jdbcUtil.closeConAndState();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		} else {
			msg = "you are not authorized";
		}
		
		retJson = retJson.replace("@err", err);
		retJson = retJson.replace("@msg", msg);
		response.getOutputStream().write(retJson.getBytes("utf-8"));
	}
}
